// index.js
// 获取应用实例
const app = getApp()
Page({

  data: {
    active: 1,
    index:null,
    historyposts:[],
    overlayShow:false,
    recommend:null,
    channel:null,
    loading:true
  },
  //页面加载
  onLoad() {
    //请求发现数据
    wx.request({
      url: 'http://api.kele8.cn/agent/https://app.vmovier.com/apiv3/index/index',
      success:(res)=>{
        // console.log(res);
        this.setData({
          index:res.data.data,
          loading:false
        })
      }
    })

    //每日推荐
    this.getDayCommend();
  },
  observers:{
    active(n){
      console.log(n);
    }
  },
  //页面触底
  onReachBottom(){
   let last = this.data.historyposts[0]?this.data.historyposts[this.data.historyposts.length-1]:this.data.index.posts;
   wx.request({
     url: 'http://api.kele8.cn/agent/https://app.vmovier.com/apiv3/index/getIndexPosts/lastid/'+last.lastid,
     success:(res)=>{
       this.setData({
        historyposts:[...this.data.historyposts,res.data.data],
       })
      // this.data.historyposts.push(res.data.data)
     }
   })
  },
  // 每日推荐获取
  getDayCommend(){
    wx.request({
      url: 'https://app.vmovier.com/apiv3/DayCover/getDayCover',
      success:(res)=>{
        // console.log(res);
        this.setData({
          recommend:res.data.data,
        })
        // console.log(this.data.recommend.image_blurred);
      }
    })
  },
  //每日推荐显示
  onClickLeft(){
    this.setData({
      overlayShow:true,
    });
    if (this.data.recommend) return;
    // wx.request({
    //   url: 'https://app.vmovier.com/apiv3/DayCover/getDayCover',
    //   success:(res)=>{
    //     // console.log(res);
    //     this.setData({
    //       recommend:res.data.data,
    //     })
    //     // console.log(this.data.recommend.image_blurred);
    //   }
    // })

  },
  //每日推荐隐藏
  closecommend() {
    this.setData({
      overlayShow:false,
    });
  },
// active切换
  onChange(event) {
    this.setData({
      active:event.detail.index
    })
    this.getchannel();
  },
  // 获取分类
  getchannel(){
    if(!this.data.channel && this.data.active==1)
    wx.request({
      url: 'http://api.kele8.cn/agent/https://app.vmovier.com/apiv3/cate/getList',
      success:(res)=>{
        this.setData({
          channel:res.data.data,
        })
        console.log(res);
      }
    })
  },
  //跳转搜索
  gotoSearch(){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  }
})
